//
//  YWInputRecordViewController.m
//  百布业务
//
//  Created by 陈炳桦 on 15/10/26.
//  Copyright © 2015年 陈炳桦. All rights reserved.
//  商户录入记录

#import "YWInputRecordViewController.h"
#import "YWInputMerchant.h"
#import "YWInputCell.h"
#import <MBProgressHUD.h>

static NSString *ID = @"ID";

@interface YWInputRecordViewController ()

// 商户录入记录数组
@property (nonatomic,strong) NSMutableArray *inputArray;

// 今天的商户录入记录数组
@property (nonatomic,strong) NSMutableArray *todayArray;

// 提示符
@property (nonatomic,strong) MBProgressHUD *HUD;

@end

@implementation YWInputRecordViewController

#pragma mark - 懒加载
- (MBProgressHUD *)HUD{
    if (_HUD == nil) {
        _HUD = [[MBProgressHUD alloc] initWithView:self.view];
        _HUD.mode = MBProgressHUDModeText;
        _HUD.margin = 20.f;
        [self.view addSubview:_HUD];
    }
    return _HUD;
}

- (NSMutableArray *)inputArray{
    if (_inputArray == nil) {
        
        NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
        NSString *filePath = [cachePath stringByAppendingPathComponent:@"inputArray.plist"];
        _inputArray = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        
        if (_inputArray == nil) {
            _inputArray = [NSMutableArray array];
        }
    }
    
    return _inputArray;
}

- (NSMutableArray *)todayArray{
    
    // 取出今天的商户录入
    if (_todayArray == nil) {
        
        _todayArray = [NSMutableArray array];
        
        for (YWInputMerchant *input in self.inputArray) {
            NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
            formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
            NSDate *date = [formatter dateFromString:input.date];
            
            if ([date isToday]) {
                [_todayArray addObject:input];
            }
        }
    }
    
    return _todayArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"商户录入记录";

    self.tableView.tableFooterView = [[UIView alloc] init];
    
    // 注册cell
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YWInputCell class]) bundle:nil] forCellReuseIdentifier:ID];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (self.todayArray.count == 0) {
        self.HUD.labelText = @"今天没有商户录入记录";
        [self.HUD show:YES];
        [self.HUD hide:YES afterDelay:2];
    }

    return self.todayArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    YWInputCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.input = self.todayArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}


@end
